import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import * as path from "path";
import viteCompression from "vite-plugin-compression";
import eslintPlugin from "vite-plugin-eslint";
import { viteVConsole } from "vite-plugin-vconsole";

// https://vitejs.dev/config/

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
	const env = loadEnv(mode, process.cwd());
	return {
		base: "/record-print-app/",
		resolve: {
			//设置别名
			alias: {
				"@": path.resolve(__dirname, "src")
			}
		},
		css: {
			preprocessorOptions: {
				scss: {
					additionalData: `@import "@/assets/css/var.scss";`
				}
			}
		},
		plugins: [
			vue(),
			// gzip压缩 生产环境生成 .gz 文件
			viteCompression({
				verbose: true,
				disable: false,
				threshold: 10240,
				algorithm: "gzip",
				ext: ".gz"
			}),
			// * EsLint 报错信息显示在浏览器界面上
			eslintPlugin(),
			// VConsole 调试工具配置
			viteVConsole({
				entry: path.resolve("src/main.js"), // 入口文件，或者可以使用这个配置: [path.resolve('src/main.js')]
				localEnabled: mode === "development", // 本地是否启用
				enabled: mode === "development", // 是否启用
				config: {
					maxLogNumber: 1000,
					theme: "light" // 主题颜色 'dark'|'light'
				}
			})
		],
		server: {
			open: false,
			cors: true,
			port: 3002,
			// host: "localhost",
			host: "0.0.0.0",
			// 设置 https 代理
			proxy: {
				"/printApi": {
					target: env.VITE_APP_BASE_API,
					changeOrigin: true,
					//ws: true,//是否启用websockets，用不到可设为false
					//secure: false, // 如果是https接口，需要配置这个参数
					rewrite: path => path.replace(/^\/printApi/, "")
				},
				"/fileApi": {
					target: "https://www.gjtool.cn",
					changeOrigin: true,
					//ws: true,//是否启用websockets，用不到可设为false
					//secure: false, // 如果是https接口，需要配置这个参数
					rewrite: path => path.replace(/^\/fileApi/, "")
				}
			}
		},
		//生产环境打包配置
		//去除 console debugger
		build: {
			minify: "terser",
			terserOptions: {
				compress: {
					drop_console: true,
					drop_debugger: true
				}
			}
		}
	};
});
